package com.hxrfid.sms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hxrfid.common.annotation.Log;
import com.hxrfid.common.context.SecurityContextHolder;
import com.hxrfid.common.core.controller.BaseController;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.domain.R;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.core.validate.AddGroup;
import com.hxrfid.common.core.validate.EditGroup;
import com.hxrfid.common.enums.BusinessType;
import com.hxrfid.common.utils.poi.ExcelUtil;
import com.hxrfid.sms.domain.bo.SysSmsRecordBo;
import com.hxrfid.sms.domain.vo.SysSmsRecordVo;
import com.hxrfid.sms.service.ISysSmsRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 短信发送记录控制器
 * 前端访问路由地址为:/sms/smsRecord
 *
 * @author hxrfid
 * @date 2022-12-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sms/smsRecord")
public class SysSmsRecordController extends BaseController {

    private final ISysSmsRecordService iSysSmsRecordService;

    /**
     * 查询短信发送记录列表
     */
    @SaCheckPermission("sms:smsRecord:list")
    @GetMapping("/list")
    public TableDataInfo<SysSmsRecordVo> list(SysSmsRecordBo bo, PageQuery pageQuery) {
        bo.setTenantId(SecurityContextHolder.getTenantId());
        return iSysSmsRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出短信发送记录列表
     */
    @SaCheckPermission("sms:smsRecord:export")
    @Log(title = "短信发送记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysSmsRecordBo bo, HttpServletResponse response) {
        List<SysSmsRecordVo> list = iSysSmsRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "短信发送记录", SysSmsRecordVo.class, response);
    }

    /**
     * 获取短信发送记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("sms:smsRecord:query")
    @GetMapping("/{id}")
    public R<SysSmsRecordVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iSysSmsRecordService.queryById(id));
    }

    /**
     * 新增短信发送记录
     */
    @SaCheckPermission("sms:smsRecord:add")
    @Log(title = "短信发送记录", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<String> add(@Validated(AddGroup.class) @RequestBody SysSmsRecordBo bo) {
        R<String> r = R.ok();
        r.setData(String.valueOf(iSysSmsRecordService.insertByBo(bo)));
        return r;
    }

    /**
     * 修改短信发送记录
     */
    @SaCheckPermission("sms:smsRecord:edit")
    @Log(title = "短信发送记录", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysSmsRecordBo bo) {
        return toAjax(iSysSmsRecordService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除短信发送记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("sms:smsRecord:remove")
    @Log(title = "短信发送记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iSysSmsRecordService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
